using System;
using System.Collections.Generic;
using System.Text;
using MCromwell.StaffIntranet.Task.Tasks;
using MCromwell.StaffIntranet.DTO;
using MCromwell.StaffIntranet.DependencyInjection;

namespace MCromwell.StaffIntranet.Presenters
{
    public class StaffViewingPresenter
    {
        private IStaffViewingTask task;
        private IStaffDetailsView view;

        public StaffViewingPresenter(IStaffDetailsView view)
            : this(IoC.Resolve<IStaffViewingTask>(), view)
        {
        }

        public StaffViewingPresenter(IStaffViewingTask task, IStaffDetailsView view)
        {
            if (task == null)
                throw new ArgumentNullException("task", "task cannot be null");

            if (view == null)
                throw new ArgumentNullException("view", "view cannot be null");

            this.task = task;
            this.view = view;

            BindViewEvents();
        }

        private void BindViewEvents()
        {
            view.StaffSelected += new StaffSelectedEventDelegate(StaffSelectedHandler);
        }
        
        private void StaffSelectedHandler(object sender, StaffSelectedEventArgs e)
        {
            StaffDTO staffSelected = task.RetrieveStaffDetailsFor(e.StaffIdSelected);

            if (staffSelected == null)
                view.DisplayError("no staff could be found for selected id");
            else
                view.ShowDetailsOf(staffSelected);
        }

        

    }
}
